package com.hjy.search.thread;

import java.util.concurrent.*;

public class main {
    public static ExecutorService threadPool =  Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main---start---");
        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行异步方法---");
            System.out.println("supplyAsync: " +Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("执行异步方法完毕");
            return i;
        }, threadPool).whenComplete((integer, throwable) -> {
            System.out.println("whenCompleteAsync:integer = " + integer);
            System.out.println("whenCompleteAsync : " + Thread.currentThread().getId());
        }).exceptionally(throwable -> {
            System.out.println("exceptionally : " + Thread.currentThread().getId());
            System.out.println("exceptionally:integer = " + 200);
            return 200;
        }).handle((integer, throwable) -> {
            System.out.println("handleAsync : " + Thread.currentThread().getId());
            return 100;
        }).handleAsync((integer, throwable) -> {
            System.out.println("handleAsync:integer = " + integer);
            return 11;
        }).handleAsync((integer, throwable) -> {
            System.out.println("handleAsync:integer = " + integer);
            return 12;
        }).thenApply(integer -> {
            System.out.println("thenApply:integer = " + integer);
            return  13;
        });

        // 获取异步任务的最终返回值
        supplyAsync.get();
        System.out.println("integer = " + supplyAsync.get());
        System.out.println("main---end---");
    }

}
